package Factory.Entities;

import Model.Core.IAchievableState;
import Model.Core.IOriginatorState;
import Model.Core.Impl.Transition;
import Model.Draw.Entities.TransitionDraw;
import Model.Draw.Events.ManifestElementEvent;
import Model.Draw.Events.ManifestStateDoneListener;
import Model.Draw.IStateDraw;
import java.util.ArrayList;
import java.util.List;

public class TransitionFactory {
    private List<ManifestStateDoneListener> listeners;

    public TransitionFactory() {
        listeners = new ArrayList<ManifestStateDoneListener>();
    }

    public void addListener(ManifestStateDoneListener listener) {
        if (!listeners.contains(listener)) {
            this.listeners.add(listener);
        }
    }

    public void removeListener(ManifestStateDoneListener listener) {
        this.listeners.remove(listener);
    }

    public void createStateGraphicalManifest(Transition transition, Object sourceElement, Object targetElement) {
        IOriginatorState originatorState = (IOriginatorState) sourceElement;
        IAchievableState achievableState = (IAchievableState) targetElement;
        transition.setOriginatorState(originatorState);
        transition.setAchievableState(achievableState);
        TransitionDraw graphicalManifest = new TransitionDraw((IStateDraw)originatorState.getDraw(), (IStateDraw)achievableState.getDraw());
        transition.setDraw(graphicalManifest);
        notifyListeners(transition);
    }
    
    private void notifyListeners(Transition transition) {
        for (ManifestStateDoneListener listener : listeners) {
            listener.manifestStateDoneHandler(new ManifestElementEvent(transition));
        }
    }

}
